package com.lfy.kcat.content.biz;

import com.lfy.kcat.content.config.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author leifengyang
 * @version 1.0
 * @date 2025/9/22 10:31
 * @description:
 */
@Slf4j
@Component
public class BloomFilterTemplate {

    @Autowired
    RedissonClient redissonClient;


    /**
     * 初始化 短剧id的布隆过滤器
     */
    public void initDramasIdBloomFilter() {
        RBloomFilter<Object> filter = redissonClient
            .getBloomFilter(RedisConst.DRAMA_BF);

        //尝试初始化布隆过滤器
        boolean b = filter.tryInit(10000000, 0.001);
        if (b){
            log.info("初始化布隆过滤器成功...");
        }else {
            log.error("布隆过滤器已经初始化过，无需重复初始化...");
        }
    }


    /**
     * 给布隆过滤器中添加一个短剧
     * @param dramaId
     */
    public void addDramaIdBloomFilter(Long dramaId) {
        RBloomFilter<Object> filter = redissonClient
            .getBloomFilter(RedisConst.DRAMA_BF);

        filter.add(dramaId);
    }


    /**
     * 判断是否包含同一个短剧id
     * @param dramaId
     * @return
     */
    public boolean containsDramaIdBloomFilter(Long dramaId) {
        RBloomFilter<Object> filter = redissonClient
            .getBloomFilter(RedisConst.DRAMA_BF);
        return filter.contains(dramaId);
    }
}
